package lego.Moving;

import java.util.Random;
import lego.animal.IAnimal;
import lego.jbv.*;

public class Wander implements Behavior{
	
	private Random random = new Random();
	private boolean suppressed = false;
	private final int wanderTime = 1000;
	private SoundController soundController;
	private IAnimal animal;

	public Wander(IAnimal animal, SoundController soundController)
	{
		this.soundController = soundController;
		this.animal = animal;
	}
	
	@Override
	public int takeControl() {
		return 10;
	}

	@Override
	public void action() {
		suppressed = false;
		int waitTime = wanderTime;
		
		Display.setStatus("WANDER");		
		
		while(!suppressed)
		{
			long time = System.currentTimeMillis();
			int toDo = random.nextInt(7);
			
			switch(toDo){
			case 0:
				Car.drive(-Display.getNormalSpeed(), Display.getNormalSpeed()); waitTime = wanderTime/2;
				soundController.playSound(animal.getWin());
				break;
			case 1:
				Car.drive(Display.getNormalSpeed(), -Display.getNormalSpeed()); waitTime = wanderTime/2; break;
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
				Car.forward(Display.getNormalSpeed(), Display.getNormalSpeed()); break;
			default:
				Car.drive(Display.getNormalSpeed(), Display.getNormalSpeed());
				break;
			}
			
			while(!suppressed && (System.currentTimeMillis() - time) < waitTime)
			{
				Thread.yield();
			}
		}
	}
	
	@Override
	public void suppress() {
		suppressed = true;
		Car.stop();
	}
}
